﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HidDevice;
//using HIDTOOL;

namespace HIDTOOL
{
    public partial class frm_HidSelect : Form
    {
        mianForm mainform;
        public frm_HidSelect(mianForm mianfrom)     //构造方法加入传入的主界面类
        {
            InitializeComponent();
            this.mainform = mianfrom;
        }

        Hid Find = new Hid();
        private void frm_HidSelect_Load(object sender, EventArgs e)
        {
            lbl_txtHidNum.Hide();
            lbl_HidNum.Hide();
            lbl_VID.Hide();
            lbl_PID.Hide();
            btn_Connection.Enabled = false;
            //获取连接的HID列表
            List<string> deviceList = new List<string>();
            Find.FindHid(ref deviceList);

            lst_HidList.Items.Clear();
            foreach (string device in deviceList)            
                lst_HidList.Items.Add(device);

            lbl_txtHidNum.Show();
            lbl_HidNum.Show();
            lbl_HidNum.Text = deviceList.Count.ToString(); 
        }

        string Current_HidDir = null;
        private void lst_HidList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lst_HidList.SelectedItem == null)
                return;
            lbl_VID.Show();
            lbl_PID.Show();
            btn_Connection.Enabled = true;
            Current_HidDir = lst_HidList.SelectedItem.ToString();
            lbl_VID.Text = Current_HidDir.Substring(12, 4).ToUpper();        //提取 HID设备地址的vID //HID范例地址：	\\?\hid#vid_1111&pid_0003#8&2f34cf51&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
            lbl_PID.Text = Current_HidDir.Substring(21, 4).ToUpper();       //提取 HID设备地址的pID
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Connection_Click(object sender, EventArgs e)
        {
            Hid.HID_RETURN ret = Hid.HID_RETURN.FAIL;
            if (Current_HidDir == null)
            {
                MessageBox.Show("请先选择HID设备", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            mainform.HID_Dir = Current_HidDir;
            ret = mainform.Open_HID();//调用 主窗口 类中的 打开HID函数

            if (ret == Hid.HID_RETURN.SUCCESS)
            {
                //需要把主界面的按键控件属性从 private 改为 public
                mainform.btn_OpenHid.Text = "断开";
                mainform.Hid_isConnected = true;
                mainform.lbl_status.Text = "已连接";
                mainform.lbl_status.ForeColor = Color.ForestGreen;
                mainform.btn_OpenHid.BackColor = Color.GreenYellow;
                ////需要把主界面的编辑框控件属性从 private 改为 public
                if(mainform.txt_Send.Text == "")
                    mainform.txt_Send.Text = "00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63";
                
                mainform.StartShowThr();
                mainform.StartSendThr();                     //HID打开成功：启动发送线程
                UpdateBTN_LBL();    //使能连接按钮，显示主界面的VID和PID值
                
                this.Close();
            }
            else                                        //设备连接失败
            {
                if (ret == Hid.HID_RETURN.DEVICE_OPENED)
                    MessageBox.Show("设备已经连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
                
                else if (ret == Hid.HID_RETURN.DEVICE_OPEN_FAIL)
                    MessageBox.Show("设备连接失败!\r\n此设备无法连接", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else if (ret == Hid.HID_RETURN.FAIL)
                    MessageBox.Show("失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                mainform.HID_Dir = null;
            }
        }

        private void lst_HidList_DoubleClick(object sender, EventArgs e)
        {
            if (lst_HidList.SelectedItem == null)
                return;
            btn_Connection_Click(null, null);
        }

        private void btn_Refresh_Click(object sender, EventArgs e)///点击刷新按钮
        {
            frm_HidSelect_Load(null, null);
        }

        private void UpdateBTN_LBL()
        {
            if (mainform.HID_Dir != null)
            {
                mainform.btn_OpenHid.Enabled = true;
                mainform.lbl_PID.Show();
                mainform.lbl_VID.Show();
                mainform.lbl_VID.Text = lbl_VID.Text;
                mainform.lbl_PID.Text = lbl_PID.Text;
            }
        }     
    }
}
